<template>
    <div class="allbox">
            <!-- 功能选择区 -->
            <div class="selection">
            <div @click="refresh(1)">
                <el-button type="info"><el-icon><RefreshRight /></el-icon></el-button>
            </div>
            <div @click="add()">
                <el-button type="success"><el-icon size="22" style="margin-right: 2px;"><Plus /></el-icon>添加</el-button>
            </div>
        </div>
        <!-- table -->
        <div class="table_box">
        <el-table  ref="ref_multipleTableRef" :data="tableData"  height="88vh" @row-dblclick="db_Click"> <!--单击 和 双击 && 单选 和全选 -->
            <el-table-column type="selection" width="50" align="left" />
            <el-table-column label="序号" width="80" type="index" align="left" sortable=""/>
            <el-table-column prop="user" label="账号" width="100"/>
            <el-table-column prop="username" label="姓名" width="140" align="left"/>
            <el-table-column prop="password" label="密码" width="140"/>
            <el-table-column prop="sex" label="性别" width="80" align="left" />
            <el-table-column prop="phone" label="联系方式" width="140" align="center" />
            <el-table-column prop="from" label="地区" width="120" align="center" />
            <el-table-column prop="money" label="余额" width="120" align="center" />
            <el-table-column fixed="right" label="执行操作" width="180" align="center">
                <template #default="scope">
                   <div>
                        <el-button type="danger" style="width: 150px;"  @click.prevent="del(scope.$index)">
                            <el-icon size="20"><DeleteFilled /></el-icon>删除
                        </el-button>
                   </div>
                </template>
                </el-table-column>
        </el-table>
        </div>
          <!-- form表单 -->
          <el-dialog
            v-model="dialogVisible"
            title="编辑表单"
            draggable
            width="35%">
            <el-form :model="form" class="demo-ruleForm" v-loading="loading" size="large">

            <el-form-item required label="姓名" label-width="120px">
                <el-input v-model="form.username" type="text" autocomplete="off"  placeholder="请输入该用户的姓名"  />
            </el-form-item>

            <el-form-item required label="账号" label-width="120px">
                <el-input v-model="form.user" type="text" autocomplete="off"  placeholder="请输入该用户的账号"  />
            </el-form-item>

            <el-form-item required label="密码" label-width="120px">
                <el-input v-model="form.password" type="text" autocomplete="off" placeholder="请输入用户的密码" />
            </el-form-item>

            <el-form-item required label="联系方式" label-width="120px">
                <el-input v-model="form.phone" type="text" autocomplete="off" placeholder="请输入该用户的电话" />
            </el-form-item>

            <el-form-item required label="地区" label-width="120px">
                <el-select
                v-model="form.from"
                class="m-2"
                placeholder="请设置该用户的地区"
                size="large"
                style="width: 240px"
                >
                <el-option
                v-for="item in provinces"
                :key="item"
                :label="item"
                :value="item"
                />
                </el-select>
            </el-form-item>

            <el-form-item required label="余额" label-width="120px">
                <el-input v-model="form.money" type="number" autocomplete="off" placeholder="请输入该用户的余额" />
            </el-form-item>

            </el-form>
        <template #footer>
            <span class="dialog-footer">
            <el-button type="primary" @click="submit()" style="width: 80%;margin-right: 8%;height: 40px;">
                {{ btn_text }}
            </el-button>
            </span>
        </template>
        </el-dialog>

        <messageComp ref="ref_messageComp"> </messageComp>
    </div>
  
</template>

<script setup>
import axios from "axios"
import { ref,getCurrentInstance } from "vue"
import { useStore } from "vuex";
import messageComp from "../components/messageComp.vue";
const ref_messageComp = ref(null) //弹窗message组件refs
const store = useStore()
const { ctx } = getCurrentInstance()
let windowsHeight = ref((window.innerHeight-60) + 'px')
// 数据
let loading = ref(false) //加载
let dialogVisible = ref(false) //表单开关
let form = ref({}) //表单数据 el-form
let tableData = ref([]) //表格数据-el-table
let btn_text = ref('添加')

let provinces = ["北京市", "上海市", "天津市", "重庆市", "河北省", "山西省", "内蒙古自治区", "辽宁省", "吉林省", "黑龙江省", "江苏省",
"浙江省", "安徽省", "福建省", "江西省", "山东省", "河南省", "湖北省", "湖南省", "广东省", "广西壮族自治区", "海南省", "四川省",
"贵省", "云南省", "西藏自治区", "陕西省", "甘肃省", "宁夏回族自治区", "青海省", "新疆维吾尔族自治区", "香港特别行政区", "澳门特别行政区",
"台湾省", "其它"]

const computeDisableFn = () => {
    if (btn_text.value == '添加') {
        return false
    } else {
        return true
    }
}

// 提交
const submit = ()=>{
    if (btn_text.value == '添加') {
        addAPI()
    } else {
        updateAPI()
    }
   
}

const db_Click = (e)=>{
    dialogVisible.value = true
    btn_text.value = '修改'
    form.value = e
}

const updateAPI = () =>{
    form.value.type = '普通用户'
    dialogVisible.value = false
    let formData =  new FormData
    formData.append('form',JSON.stringify(form.value))
    axios({
        method:'post',
        url:'/api/users/update',
        data:formData,
    }).then(res=>{
        if (res.data.code == 200) {
            ref_messageComp.value.messageSuccess('您更新了一条记录')
            refresh()
        }else{
            ref_messageComp.value.messageFail('更新失败')
        }
    }).catch(err=>{
        console.log(err);
        ref_messageComp.value.messageNetFail('网络连接失败，请检查服务器')
    })
}
const addAPI = ()=>{
    form.value.type = '普通用户'
    dialogVisible.value = false
    let formData =  new FormData
    formData.append('form',JSON.stringify(form.value))
    axios({
        method:'post',
        url:'/api/users/insert',
        data:formData,
    }).then(res=>{
        if (res.data.code == 200) {
            ref_messageComp.value.messageSuccess('您新增了一条记录')
            refresh()
        }else{
            ref_messageComp.value.messageFail('新增失败')
        }
    }).catch(err=>{
        console.log(err);
        ref_messageComp.value.messageNetFail('网络连接失败，请检查服务器')
    })
}
// 获取用户列表
const getData = ()=>{
    axios({
        url:'/api/users'
    }).then(res=>{
        console.log(res);

        if (res.data.code == 200) {

            // 寻找类型为普通用户的数据
            let arr = res.data.data.filter(item=>item.type == '普通用户') 
            tableData.value = arr

        }
    }).catch(err=>{
        ref_messageComp.value.messageNetFail('网络连接失败，请检查服务器')
    })
}

// 删除
const del = (index)=>{
    loading.value = true
    let timer = setInterval(() => {
            axios({
            method:'post',
            url:'/api/users/delete',
            data:{
                id:tableData.value[index].id
            },
        }).then(res=>{
            if (res.data.code == 200) {
                ref_messageComp.value.messageSuccess('您删除了一条记录')
                refresh()
            }else{
                ref_messageComp.value.messageFail('删除失败！')
            }
        }).catch(err=>{
            ref_messageComp.value.messageNetFail('网络连接失败，请检查服务器')
        })
        loading.value = false
        clearInterval(timer)
    }, 1000);

}
// 刷新
const refresh = (n)=>{
    setTimeout(()=>{
        if (n) {
            ref_messageComp.value.messageSuccess('刷新成功')
        }
        getData()
    },300)
}
const add = ()=>{
    form.value = {}
    dialogVisible.value = true
    btn_text.value = '添加'

}
// 执行区
refresh()
</script>


<style scoped>
.selection{
    margin: 10px;
    width: 100%;
    height: 40px;
    line-height: 40px;
    /* border: 2px solid; */
    display: flex;

}
.selection>div{
    width: 70px;
    height: 100%;
    margin-left: 10px;
    border-radius: 5px;
}
/* 刷新 */
.selection>div:nth-child(1){
    margin-left: 40px;
}



</style>